﻿Imports System.IO
Imports System.Environment

Public Class HistoryModule
    'History Write Module
    Public Sub Write(ByRef URL As String, ByVal sameurl As Boolean)
        If sameurl = False Then
            Dim fFile As New FileInfo(Environment.GetFolderPath(SpecialFolder.ApplicationData) + "\Phenix\Scavenger\User Data\history.svg")
            If fFile.Exists Then
                If Not URL.Contains("about") Then
                    Dim histwrite As New StreamWriter(Environment.GetFolderPath(SpecialFolder.ApplicationData) + "\Phenix\Scavenger\User Data\history.svg", True)
                    histwrite.WriteLine(URL)
                    histwrite.Close()
                End If
            Else
                Directory.CreateDirectory(Environment.GetFolderPath(SpecialFolder.ApplicationData) + "\Phenix\Scavenger\User Data\")
                fFile.Create()
                Dim histwrite As New StreamWriter(Environment.GetFolderPath(SpecialFolder.ApplicationData) + "\Phenix\Scavenger\User Data\history.svg", True)
                histwrite.WriteLine(URL)
                histwrite.Close()
            End If
        End If
    End Sub

    'History Read Module
    Public Sub AutoComplete_Read()
        Dim fFile As New FileInfo(Environment.GetFolderPath(SpecialFolder.ApplicationData) + "\Phenix\Scavenger\User Data\history.svg")
        If fFile.Exists Then
            If Scavenger.URL.AutoCompleteCustomSource IsNot Nothing Then
                Scavenger.URL.AutoCompleteCustomSource.Clear()
                Dim histread As New StreamReader(Environment.GetFolderPath(SpecialFolder.ApplicationData) + "\Phenix\Scavenger\User Data\history.svg")
                Scavenger.URL.AutoCompleteCustomSource.AddRange(histread.ReadToEnd.Split())
                histread.Close()
            End If
        End If
    End Sub
    Public Sub Generic_Read()
        Dim fFile As New FileInfo(Environment.GetFolderPath(SpecialFolder.ApplicationData) + "\Phenix\Scavenger\User Data\history.svg")
        If fFile.Exists Then
            Scavenger.URL.Items.Clear()
            Dim histread As New StreamReader(Environment.GetFolderPath(SpecialFolder.ApplicationData) + "\Phenix\Scavenger\User Data\history.svg")
            Dim histarray As New List(Of String)
            histarray.AddRange(histread.ReadToEnd.Split())
            histread.Close()
            Dim histline As String
            For Each histline In histarray
                History_Parser(histline)
            Next
        End If
    End Sub
    Public Sub History_Parser(ByVal Line As String)
        If Scavenger.URL.Items.Count <> 0 Then
            If Not Scavenger.URL.Items.Contains(Line) Then
                If Line <> "" Then
                    Scavenger.URL.Items.Add(Line)
                End If
            End If
        Else
            If Line <> "" Then
                Scavenger.URL.Items.Add(Line)
            End If
        End If
    End Sub
End Class
